/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swing.formularios;

import hibernate.dao.impl.ProveedorDAOImpl;
import hibernate.entidades.Productos;
import hibernate.entidades.Proveedores;
import java.awt.Component;
import java.math.BigDecimal;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import org.hibernate.exception.DataException;
import swing.InterfazPrincipal;
import swing.modelos.ComboBoxModelo;
import swing.modelos.ModeloProductos;

/**
 *
 * @author ariel
 */
public class FormProductos extends javax.swing.JDialog {

    private final ModeloProductos modeloProductos;
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("swing.formularios/Bundle");

    /**
     * Creates new form FormProductos
     */
    public FormProductos(ModeloProductos modeloProductos, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.modeloProductos = modeloProductos;
        initComponents();
        getRootPane().setDefaultButton(botonNew);
        comboProveedor.setModel(new ComboBoxModelo(new ProveedorDAOImpl()));
    }

    public FormProductos(ModeloProductos modeloProductos, InterfazPrincipal aThis, boolean b, int selectedRow) {
        this(modeloProductos, aThis, b);
        this.campoCodigo.setEditable(false);
        Productos producto = modeloProductos.getRow(selectedRow);
        this.campoCodigo.setText(producto.getCodStock());
        this.campoNombre.setText(producto.getNombre());
        this.campoStock.setValue(producto.getStock());
        this.campoPrecio.setText(String.valueOf(producto.getPrecio()));
        this.campoDescripcion.setText(producto.getDescripcion());
        this.comboProveedor.setSelectedItem(producto.getCodProv());
        botonNew.setText(bundle.getString("textoBotonEditar"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonCanel = new javax.swing.JButton();
        etiquetaTituloProducto = new javax.swing.JLabel();
        panelFormulario = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        campoCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        campoStock = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        campoPrecio = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        comboProveedor = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoDescripcion = new javax.swing.JTextArea();
        botonDelete = new javax.swing.JButton();
        botonNew = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        botonCanel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancel.png"))); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("swing/formularios/Bundle"); // NOI18N
        botonCanel.setText(bundle.getString("CANCELAR")); // NOI18N
        botonCanel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCanelActionPerformed(evt);
            }
        });

        etiquetaTituloProducto.setFont(new java.awt.Font("Trajan Pro", 0, 24)); // NOI18N
        etiquetaTituloProducto.setForeground(new java.awt.Color(153, 51, 0));
        etiquetaTituloProducto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaTituloProducto.setText(bundle.getString("formProducto.etiquetaTituloProducto.text")); // NOI18N

        panelFormulario.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("formProducto.panelFormulario.border.title"))); // NOI18N

        jLabel1.setText(bundle.getString("formProducto.jLabel1.text")); // NOI18N

        jLabel2.setText(bundle.getString("formProducto.jLabel2.text")); // NOI18N

        jLabel3.setText(bundle.getString("formProducto.jLabel3.text")); // NOI18N

        jLabel4.setText(bundle.getString("formProducto.jLabel4.text")); // NOI18N

        jLabel5.setText(bundle.getString("formProducto.jLabel5.text")); // NOI18N

        jLabel6.setText(bundle.getString("formProducto.jLabel6.text")); // NOI18N

        jLabel7.setText(bundle.getString("formProducto.jLabel7.text")); // NOI18N

        campoDescripcion.setColumns(20);
        campoDescripcion.setLineWrap(true);
        campoDescripcion.setRows(5);
        jScrollPane1.setViewportView(campoDescripcion);

        javax.swing.GroupLayout panelFormularioLayout = new javax.swing.GroupLayout(panelFormulario);
        panelFormulario.setLayout(panelFormularioLayout);
        panelFormularioLayout.setHorizontalGroup(
            panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormularioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1)
                    .addComponent(campoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(campoNombre)
                    .addGroup(panelFormularioLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(campoStock, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4)
                    .addGroup(panelFormularioLayout.createSequentialGroup()
                        .addComponent(campoPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addComponent(jLabel6)
                    .addComponent(comboProveedor, 0, 165, Short.MAX_VALUE))
                .addGap(51, 51, 51)
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelFormularioLayout.setVerticalGroup(
            panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormularioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panelFormularioLayout.createSequentialGroup()
                        .addComponent(campoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(campoStock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botonDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/gnome_edit_delete.png"))); // NOI18N
        botonDelete.setText(bundle.getString("BORRAR")); // NOI18N
        botonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDeleteActionPerformed(evt);
            }
        });

        botonNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/add.png"))); // NOI18N
        botonNew.setText(bundle.getString("CREAR")); // NOI18N
        botonNew.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        botonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panelFormulario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botonNew)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCanel)))
                .addContainerGap())
            .addComponent(etiquetaTituloProducto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(etiquetaTituloProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelFormulario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonNew)
                    .addComponent(botonDelete)
                    .addComponent(botonCanel))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-471)/2, (screenSize.height-446)/2, 471, 446);
    }// </editor-fold>//GEN-END:initComponents

    private void botonCanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCanelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_botonCanelActionPerformed

    private void botonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDeleteActionPerformed
        // TODO add your handling code here:
        Component[] components = panelFormulario.getComponents();
        for (int i = 0; i < components.length; i++) {
            if (components[i].getClass() == campoCodigo.getClass()) {
                ((JTextField) components[i]).setText("");
            }
        }
    }//GEN-LAST:event_botonDeleteActionPerformed

    private void botonNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNewActionPerformed
        try {
            // TODO add your handling code here:
            Productos producto;
            producto = new Productos(campoCodigo.getText());
            producto.setNombre(campoNombre.getText());
            producto.setDescripcion(campoDescripcion.getText());
            producto.setStock(Short.parseShort(campoStock.getValue().toString()));
            if (campoPrecio.getText() != "") {
                producto.setPrecio(Double.valueOf(campoPrecio.getText()));
            }
            producto.setCodProv((Proveedores) comboProveedor.getSelectedItem());


            /**
             * obtener el owner modeloClientes.
             */
            this.modeloProductos.addRow(producto);
            this.modeloProductos.fireTableDataChanged();
            this.dispose();
        } catch (DataException | NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_botonNewActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCanel;
    private javax.swing.JButton botonDelete;
    private javax.swing.JButton botonNew;
    private javax.swing.JTextField campoCodigo;
    private javax.swing.JTextArea campoDescripcion;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoPrecio;
    private javax.swing.JSpinner campoStock;
    private javax.swing.JComboBox comboProveedor;
    private javax.swing.JLabel etiquetaTituloProducto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelFormulario;
    // End of variables declaration//GEN-END:variables
}
